import org.toonWatch.*

class BootStrap {

    def springSecurityService

    def init = { servletContext ->
        
        //create roles (users and admin)
        def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
        def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
        
        //create admin user
        def adminUser = User.findByUsername('admin') ?: new User(
        //def adminUser = new User(
            username: 'admin',
            password: 'admin',
            enabled: true,
            name: 'mename',
            lastname: 'melastname',
            address1: 'adasdasdsaasda', address2: 'sdasdasdssad',
            postal: '123456', phonenumber: '12345667', birthday:'10/05/2011').save(failOnError: true)
        //password was hashed; As of version 1.2 of the Spring Security Core plugin, the generated User class automatically encodes the password when an instance is saved. Hence you no longer need to explicitly use SpringSecurityService.encodePassword() 

        if (!adminUser.authorities.contains(adminRole)) {
            UserRole.create adminUser, adminRole
    }
/*        
        //create category
        def testCat = new Category (name:'Superman')
        testCat.save()
        def testCat2 = new Category (name:'Spiderman')
        testCat2.save()
        def testCat3 = new Category (name:'Domokun')
        testCat3.save()
        def testCat4 = new Category (name:'Batman')
        testCat4.save()
            */
    }

    def destroy = {
    }
}
